<?
$smarty=new Smarty();
if(!ANTIHACK) die("ANTIHACK...");
$smarty->assign("nameact","Sửa quảng cáo");
if(isset($_GET['do']) && $_GET['do']=="del" && intval($_GET['id'])>0 )
{
	$query=query("delete from `quangcao` where id=".intval($_GET['id']));
	if($query)
		header("location:".base_admin."?act=quang-cao");
	else
		$smarty->assign("msg",$msg["error"]);
}

if($_POST['save'])
{
	$id=intval($_POST['id']);
	if($id<=0)
		header("location:index.php");
	$name=$_POST['name'];
	$height=$_POST['height'];
	$position=$_POST['position'];
	$image=$_POST['image'];
	$url=$_POST['url'];
	$sort=intval($_POST['sort']);
	foreach($image as $key=>$value)
	{
		if(is_numeric($key))
		{
			if($value!="")
			{
				$udata=array("img"=>$value,"url"=>$url[$key]);
				update($udata,"link_quangcao","id=".$key);
			}
			else
			{
				query("DELETE FROM link_quangcao WHERE id=".$key);
			}
		}
	}
	foreach($image['new'] as $key => $value)
	{
		if($value!="")
		{
			$udata=array("img"=>$value,"url"=>$url['new'][$key],"parent"=>$id);
			insert($udata,"link_quangcao");
		}
	}
	$content=addslashes(json_encode(array($image,$url),true));
	$data=array(
			"name"=>$name,
			"height"=> $height,
			"position" =>$position,
			"sort" =>$sort,
			"content"=>$content
		);
	$ret=update($data,"quangcao","id=".$id);
	if($ret)
		$smarty->assign("msg","Lưu thành công"."<script>setTimeout('location.href=\"".base_admin."?act=quang-cao\"',1000);</script>");
	else
		$smarty->assign("msg","Có lỗi xảy ra");
}
$sel=query("select * from quangcao where id=".$_GET['id']);
$fsel=fetch($sel);
$getqc=query("select * from link_quangcao where parent=".$fsel['id']);
while($fgetqc=fetch($getqc))
{
	$dataQC[]=array('id'	=> $fgetqc['id'],
					'img'	=> $fgetqc['img'],
					'url'	=> $fgetqc['url']);
}
$data=$fsel;
$data[content]=$dataQC;
$q=query("select * from other where code='position-ads'");
$pos=fetch($q);
$pos=json_decode($pos["content1"]);
$smarty->assign("pos",$pos);
$smarty->assign("data",$data);


$smarty->display("edit-quangcao.htm");
?>